Bug 1544

Bug Description:

This is a starvation bug.
1) In main thread, addIndexes(IndexReader[]) acquires the write lock, then begins optimization of destination index.
2) Main thread starts a ConcurrentMergeScheduler.MergeThread to merge the 2 segments.
3) Merging thread tries to acquire the read lock at IndexWriter.blockAddIndexes(boolean) in IndexWriter.StartCommit(), but cannot as...
4) Main thread still holds the write lock, and is waiting for the runningMerges data structure to be devoid of merges with their optimize flag set.
Both working threads are waiting for each other, as shown in stack trace below:

	
"Lucene Merge Thread #0" daemon prio=6 tid=0x0000000006efe000 nid=0x2ad0 in Object.wait() [0x0000000007e5f000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007d6372780> (a org.apache.lucene.index.IndexWriter)
at org.apache.lucene.index.IndexWriter.doWait(IndexWriter.java:4491)
- locked <0x00000007d6372780> (a org.apache.lucene.index.IndexWriter)
at org.apache.lucene.index.IndexWriter.acquireRead(IndexWriter.java:376)
- locked <0x00000007d6372780> (a org.apache.lucene.index.IndexWriter)
at org.apache.lucene.index.IndexWriter.blockAddIndexes(IndexWriter.java:2811)
at org.apache.lucene.index.IndexWriter.startCommit(IndexWriter.java:4533)
- locked <0x00000007d6372780> (a org.apache.lucene.index.IndexWriter)
at org.apache.lucene.index.IndexWriter.commit(IndexWriter.java:3342)
at org.apache.lucene.index.IndexWriter.mergeMiddle(IndexWriter.java:4317)
at org.apache.lucene.index.IndexWriter.merge(IndexWriter.java:3877)
at org.apache.lucene.index.ConcurrentMergeScheduler.doMerge(ConcurrentMergeScheduler.java:205)
at org.apache.lucene.index.ConcurrentMergeScheduler$MergeThread.run(ConcurrentMergeScheduler.java:260)

"main" prio=6 tid=0x000000000054c800 nid=0x1bac in Object.wait() [0x000000000204e000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007d6372780> (a org.apache.lucene.index.IndexWriter)
at org.apache.lucene.index.IndexWriter.doWait(IndexWriter.java:4491)
- locked <0x00000007d6372780> (a org.apache.lucene.index.IndexWriter)
at org.apache.lucene.index.IndexWriter.optimize(IndexWriter.java:2268)
- locked <0x00000007d6372780> (a org.apache.lucene.index.IndexWriter)
at org.apache.lucene.index.IndexWriter.optimize(IndexWriter.java:2203)
at org.apache.lucene.index.IndexWriter.optimize(IndexWriter.java:2183)
at org.apache.lucene.index.IndexWriter.addIndexes(IndexWriter.java:3165)
at org.apache.lucene.Test1544.testAddIndexesByIndexReader(Test1544.java:88)

More details about this bug are at LUCENE-1544 JIRA page.

How To Reproduce:

This bug is reproduced under lucene 2.4.0 and JDK 1.6.0_33.
Execute the following scripts to run the test to reproduce the bug (assume the location of the lucene test project is lucene_test_home).

Linux:
${lucene_test_home}/scripts/1544.sh
Windows:
%lucene_test_home%\scripts\1544.bat